package com.wuzf.navigation_test.utils;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.AssetManager;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wuzf.navigation_test.model.Destination;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class AppConfig {

    private static final String FILE_NAME = "destination.json";

    public static void builder(FragmentActivity activity,
                               NavController controller,
                               int containerId) {
        NavigatorProvider provider = controller.getNavigatorProvider();
        ActivityNavigator activityNavigator = provider.getNavigator(ActivityNavigator.class);
        FixFragmentNavigator fragmentNavigator = new FixFragmentNavigator(activity, activity.getSupportFragmentManager(), containerId);
        provider.addNavigator(fragmentNavigator);

        NavGraph graph = new NavGraph(new NavGraphNavigator(provider));

        Map<String, Destination> map = formatJson(FILE_NAME);
        if (map != null && map.size() > 0) {
            for (Destination destination : map.values()) {
                if (destination.getActivity()) {
                    ActivityNavigator.Destination aDest = activityNavigator.createDestination();
                    aDest.setId(destination.getId());
                    aDest.setComponentName(
                            new ComponentName(MyApplication.getApplication().getPackageName(),
                                    destination.getClassName()));
                    aDest.addDeepLink(destination.getPageUrl());
                    graph.addDestination(aDest);
                } else {
                    FragmentNavigator.Destination aDest = fragmentNavigator.createDestination();
                    aDest.setId(destination.getId());
                    aDest.setClassName(destination.getClassName());
                    aDest.addDeepLink(destination.getPageUrl());
                    graph.addDestination(aDest);
                }
                if (destination.getStart()) {
                    graph.setStartDestination(destination.getId());
                }
            }
        }
        controller.setGraph(graph);
    }

    public static String parseJson(String fileName) {
        AssetManager assetManager = MyApplication.getApplication().getAssets();
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            InputStream is = assetManager.open(fileName);
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static Map<String, Destination> formatJson() {
        return formatJson(FILE_NAME);
    }

    private static Map<String, Destination> formatJson(String fileName) {
        Map<String, Destination> map = new HashMap<>();
        String resultJson = parseJson(fileName);
        if (resultJson != null && resultJson != "") {
            map = JSON.parseObject(resultJson, new TypeReference<HashMap<String, Destination>>() {
            });
        }
        return map;
    }
}
